export const mixinList = (options = {}) => {
  const {
    params: $params,
    pagination: $pagination,
    pages: $pages,
    getList: $getList,
    getParams: $getParams,
    initOnCreate: $initOnCreate,
    useMock: $useMock // 使用mock数据,开发时快速画页面
  } = options

  const usePagination = $pagination !== false
  const initOnCreate = $initOnCreate !== false

  const _initParamsString = JSON.stringify($params || {})
  const initParams = () => {
    return JSON.parse(_initParamsString)
  }
  const mixin = {
    data() {
      return {
        loading: false,
        isLoaded: false,
        isFinished: false,
        params: initParams(),
        list: [],
        pages: {
          page: 1,
          pageSize: 5,
          total: 0,
          ...($pages || {})
        }
      }
    },
    computed: {

    },
    methods: {
      search() {
        this.pages.page = 1
        this.isFinished = false
        this.getList()
      },
      reset() {
        this.params = initParams()
        this.search()
      },
      getList() {
        if (typeof $getList !== 'function') return console.error('getList is not function')
        const params = this.getParams()
        // TODO: 开发时使用
        if ($useMock) return getMockDataList(this)
        // TODO: 开发时使用 后期移除 end
        this.loading = true
        return $getList.call(this, params)
          .then((res) => {
            if (usePagination) {
              const _list = res.data?.list || []
              if (this.pages.page === 1) {
                this.list = []
                this.$nextTick(() => {
                  this.list = _list
                })
              } else {
                this.list.push(..._list)
              }
              if (this.pages.page > _list.length) {
                this.isFinished = true
              } else {
                this.isFinished = false
              }
              this.pages.total = res.data?.total
            } else {
              this.list = res.data || []
            }
            if (typeof $getListCallback === 'function') $getListCallback.call(this)
            setTimeout(() => {
              this.loading = false
            }, 160)
            return res
          })
          .catch((e) => {
            this.loading = false
            return Promise.reject(e)
          }).finally(() => {
            this.isLoaded = true
          })
      },
      // loadMore
      loadMore() {
        if (this.loading || this.isFinished) return
        ++this.pages.page
        this.getList()
      },
      // 可重置, 查询参数获取
      getParams() {
        const _params = typeof $getParams === 'function' ? $getParams.call(this, this.params || {}) : this.params
        const { ...params } = _params || {}
        if (usePagination) {
          params.page = this.pages.page
          params.pageSize = this.pages.pageSize
        }
        return params
      },
    },
    created() {
      // 方法别名
      this.update = this.getList
    },
    onLoad() {
      // 初始化时调用
      if (initOnCreate) {
        this.$nextTick(this.getList)
      }
    }
  }
  return mixin
}

// 开发时用于mock数据, 快速呈现页面
const getMockDataList = (vm) => {
  const list = Array(vm.pages?.size || 10).fill(1).map((o, i) => ({
    id: i,
    name: 'name' + i,
    date: dayjs().format('YYYY-MM-DD'),
    dateTime: dayjs().format('YYYY-MM-DD hh:mm:ss'),
    number: 10 + i,
    // 党员字段
    userName: 'userName' + i,
    sex: i % 2,
    phonenumber: '1381381381' + (i % 11),
    userType: i % 3,
    userStatus: i % 2,
    ethnicGroup: i % 56,
    partyDate: dayjs().format('YYYY-MM-DD'), // 入党日期
    toFullMembersDate: dayjs().format('YYYY-MM-DD'), // 转正日期
    // 党员字段 end
    k1: 'k1-' + i,
    k2: 'k2-' + i,
    k3: 'k3-' + i,
    k4: 'k4-' + i,
    k5: 'k5-' + i,
    k6: 'k6-' + i,
    k7: 'k7-' + i,
    k8: 'k8-' + i,
    k9: 'k9-' + i,
    k10: 'k10-' + i,
    k11: 'k11-' + i,
    k12: 'k12-' + i,
    k13: 'k13-' + i,
    k14: 'k14-' + i,
    k15: 'k15-' + i,
    k16: 'k16-' + i,
  }))

  vm.list = list
  vm.pages.total = 180
}